﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OrderAggregator.DataStorage;

namespace OrderAggregator.UI
{
    public static class UIManager
    {
        /// <summary>
        /// Get table title
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        public static string GetTableTitle(System.Data.DataTable table)
        {
            if (table is Storage.OrganizationDataTable)
            {
                return "Организации";
            }
            else if (table is Storage.DepartmentDataTable)
            {
                return "Цеха";
            }
            else if (table is Storage.ProductDataTable)
            {
                return "Продукты";
            }
            else if (table is Storage.OrganizationProductRelationDataTable)
            {
                return "Связи Организация-Продукт";
            }
            else if (table is Storage.DiscountDataTable)
            {
                return "Скидки, накрутки,..";
            }
            else if (table is Storage.FormulaDataTable)
            {
                return "Формулы";
            }

            return table.TableName;
        }

        /// <summary>
        /// Create list control
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        public static BaseEditListControl CreateListControl(DataTable table)
        {
            if (table is DataStorage.Storage.OrganizationDataTable)
            {
                var control = new OrganizationListControl();
                control.Init((DataStorage.Storage.OrganizationDataTable)table);
                return control;
            }
            else
            {
                var control = new TableEditControl();
                control.Table = table;
                return control;
            }
             
        }

        /// <summary>
        /// Get icon
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static System.Drawing.Icon GetIcon(string name)
        {
            var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(
                "OrderAggregator.UI.Icons." + name
                );

            using (stream)
            {
                return new Icon(stream);
            }
        }
        
        /// <summary>
        /// Edit formula
        /// </summary>
        /// <param name="formula"></param>
        /// <returns></returns>
        public static bool EditFormula(ref string formula)
        {
            var editor = new FormulaEditControl();
            editor.Formula = formula;

            using (var form = new EditDialog())
            {
                form.Icon = UIManager.GetIcon("Formula.ico");
                form.AcceptButton = null;

                form.Text = "Формула";
                form.HostedControl = editor;
                if (form.ShowDialog() == DialogResult.OK)
                {
                    formula = editor.Formula;
                    return true;
                }
            }

            return false;
        }
    }
}
